package re

import (
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var (
	Content404 = `
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <title>Server Error</title>
        <style>
            html {
                -webkit-text-size-adjust: 100%;
                font-size: 125%;
            }

            body {
                background-color: #f7f7f7;
                color: #646464;
                font-family: "Segoe UI", Tahoma, sans-serif;
                font-size: 75%;
            }

            .interstitial-wrapper {
                box-sizing: border-box;
                font-size: 1em;
                line-height: 1.6em;
                margin: 100px auto 0;
                max-width: 600px;
                width: 100%;
            }

            .icon {
                background-repeat: no-repeat;
                background-size: 100%;
                height: 72px;
                margin: 0 0 40px;
                width: 72px;
                -webkit-user-select: none;
                display: inline-block;
            }

            .icon-generic {
                /** * Can't access chrome://theme/IDR_ERROR_NETWORK_GENERIC from an untrusted * renderer process, so embed the resource manually. */
                content: -webkit-image-set( url() 1x, url() 2x);
            }

            h1 {
                color: #333;
                font-size: 1.6em;
                font-weight: normal;
                line-height: 1.25em;
                margin-bottom: 16px;
                margin-top: 0;
                word-wrap: break-word;
            }

            #main-message > p {
                display: inline;
            }

            .error-code {
                color: #696969;
                display: block;
                font-size: .8em;
                margin-top: 15px;
                opacity: .5;
                text-transform: uppercase;
            }

            @media (max-height: 600px) {
                .error-code {
                    margin-top: 10px;
                }
            }

            @media (max-width: 420px) and (max-height: 736px) and (orientation: portrait), (max-width: 736px) and (max-height: 420px) and (orientation: landscape) {
                body {
                    margin: 0 auto;
                }

                .error-code {
                    margin-top: 0;
                }

                h1 {
                    font-size: 1.5em;
                    margin-bottom: 8px;
                }

                .icon {
                    margin-bottom: 12px;
                }

                .interstitial-wrapper {
                    box-sizing: border-box;
                    margin: 24px auto 12px;
                    padding: 0 24px;
                    position: relative;
                }

                .interstitial-wrapper p {
                    font-size: .95em;
                    line-height: 1.61em;
                    margin-top: 8px;
                }

                #main-content {
                    margin: 0;
                    transition: opacity 100ms cubic-bezier(0.4, 0, 0.2, 1);
                }
            }

            @media (min-width: 375px) and (min-height: 641px) and (max-height: 736px) and (max-width: 414px) and (orientation: portrait) {
                body:not(.offline) .icon {
                    height: 80px;
                    width: 80px;
                }

                h1 {
                    font-size: 1.7em;
                }

                .icon {
                    margin-bottom: 28px;
                }

                .interstitial-wrapper {
                    padding: 28px;
                }

                .interstitial-wrapper p {
                    font-size: 1.05em;
                }
            }

            @media (min-height: 500px) and (max-width: 414px) and (orientation: portrait) {
                .interstitial-wrapper {
                    margin-top: 96px;
                }
            }

            @media (min-width: 680px) and (max-height: 414px) {
                .interstitial-wrapper {
                    margin: 24px auto;
                }
            }

            @media (max-height: 400px) and (orientation: portrait), (max-height: 239px) and (orientation: landscape), (max-width: 419px) and (max-height: 399px) {
                .interstitial-wrapper {
                    display: flex;
                    flex-direction: column;
                    margin-bottom: 0;
                }

                #main-content {
                    flex: 1 1 auto;
                    order: 0;
                }
            }

            @media (max-height: 350px) {
                h1 {
                    margin: 0 0 15px;
                }

                .interstitial-wrapper {
                    margin-top: 5%;
                }
            }

            @media (max-height: 400px) and (orientation: portrait), (max-height: 239px) and (orientation: landscape), (max-width: 419px) and (max-height: 399px) {
                .interstitial-wrapper {
                    display: flex;
                    flex-direction: column;
                    margin-bottom: 0;
                }

                #main-content {
                    flex: 1 1 auto;
                    order: 0;
                }
            }

            @media (max-height: 240px) and (orientation: landscape), (max-height: 480px) and (orientation: portrait), (max-width: 419px) and (max-height: 323px) {
                body:not(.offline) .icon {
                    height: 56px;
                    width: 56px;
                }

                .icon {
                    margin-bottom: 16px;
                }
            }

            @media (max-height: 240px) and (orientation: landscape), (max-height: 480px) and (orientation: portrait), (max-width: 419px) and (max-height: 323px) {
                body:not(.offline) .icon {
                    height: 56px;
                    width: 56px;
                }

                .icon {
                    margin-bottom: 16px;
                }
            }

            @media (max-width: 640px), (max-height: 640px) {
                h1 {
                    margin: 0 0 15px;
                }
            }

            @media (max-height: 350px) {
                h1 {
                    margin: 0 0 15px;
                }

                .interstitial-wrapper {
                    margin-top: 5%;
                }
            }
        </style>
    </head>
    <body>
        <div id="main-frame-error" class="interstitial-wrapper">
            <div id="main-content">
                <div class="icon icon-generic"></div>
                <div id="main-message">
                    <h1>{{.Title}} </h1>
                    <p>{{.Desc}}.</p>
					</br>
					<p>{{.Method}} {{.RequestURI}}</p>
                    <div class="error-code">{{.ERR}}</div>
                </div>
            </div>
        </div>
    </body>
</html>
`
)

const (
	ErrOk            = 200
	ErrParam         = 400
	ErrUnAuthed      = 401
	ErrPermission    = 403
	ErrNotFound      = 404
	ErrInternalError = 500
	ErrFail          = 500
)

var (
	RESPONSE_TYPE = 0 //返回json结构的类型 0:  错误码在HTTP STATUS里, json里的code字段, 成功SUCCESS 失败 FAIL
	//RESPONSE_TYPE = 1  //返回json结构的类型 1:  HTTP STATUS 都为 200, json里的code字段, 为错误码
)

// 数据返回通用JSON数据结构
type JsonResponse struct {
	Code    string      `json:"code"` // 错误码((0:成功, 1:失败, >1:错误码))
	Message string      `json:"msg"`  // 提示信息
	Count   int64       `json:"count"`
	Data    interface{} `json:"data"` // 返回数据(业务接口定义具体数据结构)
}

// 标准返回结果数据结构封装。
func Json(r *ghttp.Request, code int, message string, count int64, data ...interface{}) {
	responseData := interface{}(nil)
	if len(data) > 0 {
		responseData = data[0]
	}
	codeStr := ""
	if RESPONSE_TYPE == 0 {
		codeStr = "SUCCESS"
		if code != ErrOk {
			codeStr = "FAIL"
		}
		r.Response.WriteHeader(code)
	} else {
		codeStr = gconv.String(code)
		r.Response.WriteHeader(ErrOk)
	}

	if count != -1 {
		r.Response.WriteJson(JsonResponse{
			Code:    codeStr,
			Count:   count,
			Message: message,
			Data:    responseData,
		})
	} else {
		r.Response.WriteJson(JsonResponse{
			Code:    codeStr,
			Count:   0,
			Message: message,
			Data:    responseData,
		})
	}

}

// 返回JSON数据并退出当前HTTP执行函数。
func DataExit(r *ghttp.Request, data interface{}) {
	Json(r, ErrOk, "", -1, data)
	r.Exit()
}

func SuccessExit(r *ghttp.Request) {
	Json(r, ErrOk, "ok", -1)
	r.Exit()
}

func DataNotExistExit(r *ghttp.Request) {
	Json(r, ErrNotFound, "数据不存在", -1)
	r.Exit()
}

func ListExit(r *ghttp.Request, count int64, list interface{}) {
	Json(r, ErrOk, "", count, list)
	r.Exit()
}

func JsonExit(r *ghttp.Request, err int, msg string, data ...interface{}) {
	Json(r, err, msg, -1, data...)
	r.Exit()
}

func ErrorExit(r *ghttp.Request, err error) {
	Json(r, ErrInternalError, err.Error(), -1)
	r.Exit()
}

func MessageExit(r *ghttp.Request, msg string) {
	Json(r, ErrOk, msg, -1)
	r.Exit()
}
func FailExit(r *ghttp.Request, msg string) {
	Json(r, ErrFail, msg, -1)
	r.Exit()
}

// 未验证身份
func UnAuthExit(r *ghttp.Request, msg string) {
	Json(r, ErrUnAuthed, msg, -1)
	r.Exit()
}

// 无权限操作
func NoPermissionExit(r *ghttp.Request, msg string) {
	Json(r, ErrPermission, msg, -1)
	r.Exit()
}

// 参数错误
func ParamErrExit(r *ghttp.Request, msg string) {
	Json(r, ErrParam, msg, -1)
	r.Exit()
}

func BodyParamErrExit(r *ghttp.Request, err error) {
	Json(r, ErrParam, err.Error(), -1)
	r.Exit()
}

func DBActionErrExit(r *ghttp.Request, err error) {
	Json(r, ErrInternalError, err.Error(), -1)
	r.Exit()
}

func GetPageSize(r *ghttp.Request) (int, int) {
	return getPage(r), getSize(r)
}

func getPage(r *ghttp.Request) int {
	tmp := r.GetQueryInt("index", -1)
	if tmp == -1 {
		tmp = r.GetQueryInt("Index", -1)
	}
	if tmp == -1 {
		tmp = r.GetQueryInt("page", -1)
	}
	if tmp == -1 {
		tmp = r.GetQueryInt("Page", -1)
	}
	if tmp == -1 {
		tmp = 1
	}
	return tmp - 1
}

func getSize(r *ghttp.Request) int {
	tmp := r.GetQueryInt("size", -1)
	if tmp == -1 {
		tmp = r.GetQueryInt("Size", -1)
	}

	if tmp == -1 {
		tmp = 10
	}
	return tmp
}
